package com.shuai.model.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.shuai.model.dto.AllotDto;
import com.shuai.model.po.SysRole;
import com.shuai.model.vo.SysRoleVo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 角色信息转换器
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
public class SysRoleConverter {
    /**
     * Po转换Vo
     * @param source
     * @return
     */
    public static SysRoleVo convertToVoByPo(SysRole source) {
        SysRoleVo target =  new SysRoleVo();
        BeanUtil.copyProperties(source, target);
        return target;
    }

    /**
     * Po转换Vo
     * @param sources
     * @return
     */
    public static List<SysRoleVo> convertToVoByPo(List<SysRole> sources) {
        List<SysRoleVo> newList = CollUtil.newArrayList();
        sources.stream().forEach(item->{
            SysRoleVo target = new SysRoleVo();
            BeanUtil.copyProperties(item, target);
            newList.add(target);
        });
        return newList;
    }

    /**
     * Po转换AllotDto
     * @param sources
     * @return
     */
    public static List<AllotDto> convertToAllotDtoByPo(List<SysRole> sources) {
        List<AllotDto> newList = CollUtil.newArrayList();
        sources.stream().forEach(item->{
            AllotDto target = AllotDto.builder()
                    .key(item.getId()).label(item.getRoleName()).labelCode(item.getRoleCode()).build();
            BeanUtil.copyProperties(item, target);
            newList.add(target);
        });
        return newList;
    }

}
